package com.financing.platform.web.api;

import java.util.Collections;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.financing.platform.common.Pager;
import com.financing.platform.common.ResultMap;
import com.financing.platform.dto.FpOrderCostDTO;
import com.financing.platform.service.IFpOrderCostService;

@Controller("fpOrderCostController")
@RequestMapping("/api/fpOrderCost")
public class FpOrderCostController extends BaseAction  {

	private Logger logger = Logger.getLogger(getClass());
	
	@Autowired
	private IFpOrderCostService fpOrderCostService;
	
	@RequestMapping("/listFpOrderCost")
	@ResponseBody
	public ResultMap list(HttpServletRequest request, FpOrderCostDTO fpOrderCost, Pager pager){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query fpOrderCost list begin .......");
			
			fpOrderCost.setBegin(pager.getStartIndex());
			fpOrderCost.setPageSize(pager.getPageSize());
			
			int count = fpOrderCostService.queryListCount(fpOrderCost);
			
			pager.setTotalCount(count);
			
			List<FpOrderCostDTO> fpOrderCostList = null;
			
			if(count > 0){
				fpOrderCostList = fpOrderCostService.queryList(fpOrderCost);
			}else{
				fpOrderCostList = Collections.emptyList();
			}
			result.setAttribute("pager", pager);
			result.setAttribute("list", fpOrderCostList);

			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query fpOrderCost list end .......");
		}catch(Exception e){
			log.error("query fpOrderCost error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);

		}
		
		return result;
	}
	
	@RequestMapping("/addFpOrderCost")
	@ResponseBody
	public ResultMap add(HttpServletRequest request, FpOrderCostDTO fpOrderCost){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("add fpOrderCost  begin .......");
			

			
			String id = fpOrderCostService.insert(fpOrderCost);
			result.setAttribute("id", id);
			

			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("add fpOrderCost list end .......");
		}catch(Exception e){
			log.error("add fpOrderCost error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);

		}
		
		return result;
	}
	
	
	@RequestMapping("/detailFpOrderCost")
	@ResponseBody
	public ResultMap detail(HttpServletRequest request, FpOrderCostDTO fpOrderCost){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("query detail fpOrderCost  begin .......");
			
			String id = fpOrderCost.getId();
			
			if(StringUtils.isBlank(id)){

				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			FpOrderCostDTO dto = fpOrderCostService.loadById(id);
			
			result.setAttribute("dto",dto);
			

			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query detail fpOrderCost  end .......");
		}catch(Exception e){
			log.error("query detail fpOrderCost error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);

		}
		
		return result;
	}
	
	@RequestMapping("/deleteFpOrderCost")
	@ResponseBody
	public ResultMap delete(HttpServletRequest request, FpOrderCostDTO fpOrderCost){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("delete fpOrderCost  begin .......");
			
			String id = fpOrderCost.getId();
			
			if(StringUtils.isBlank(id)){

				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			
			fpOrderCostService.delete(id);
			

			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("delete fpOrderCost  end .......");
		}catch(Exception e){
			log.error("delete fpOrderCost error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);

		}
		
		return result;
	}
	
	@RequestMapping("/updateFpOrderCost")
	@ResponseBody
	public ResultMap update(HttpServletRequest request, FpOrderCostDTO fpOrderCost){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("update fpOrderCost  begin .......");
			
			String id = fpOrderCost.getId();
			
			if(StringUtils.isBlank(id)){

				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}
			

			
			fpOrderCostService.update(fpOrderCost);
			

			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("update fpOrderCost list end .......");
		}catch(Exception e){
			log.error("update fpOrderCost error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);

		}
		
		return result;
	}
	
	@RequestMapping("/updateForceFpOrderCost")
	@ResponseBody
	public ResultMap updateForce(HttpServletRequest request, FpOrderCostDTO fpOrderCost){
		ResultMap result = new ResultMap();
		
		try{
			logger.info("updateForce fpOrderCost  begin .......");
			
			String id = fpOrderCost.getId();
			
			if(StringUtils.isBlank(id)){

				result.setStatus(ResultMap.STATUS.FAILE);
				return result;
			}

			fpOrderCostService.updateForce(fpOrderCost);

			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("updateForce fpOrderCost list end .......");
		}catch(Exception e){
			log.error("updateForce fpOrderCost error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);

		}
		
		return result;
	}
	
}
